# File:     descriptive_barplot_interrupted_interruptor.R
# Input:    /Data/finalData.RData
# Output:   /Paper/Figures/utterances_interruptions_bar.pdf
#           /Paper/Figures/utterances_interruptions_bar_SI.pdf
# Author:   JB


rm(list = ls())
require(tidyverse)
require(ggridges)

load('./data/finalData.RData')

toplot1 <- utterance_level %>%
  # mutate(interrupted = ifelse(grepl('--$',textclean),1,0)) %>%
  group_by(position,party,gender,name) %>%
  summarise(all = n(),
            interrupted = sum(interrupted)) %>%
  ungroup() %>%
  mutate(propInterrupt = interrupted/all,
         all =  all - interrupted) %>%
  filter(all > 100) %>%
  mutate(id = paste0(str_to_title(name),' (',position,': ',party,'-',gender,')')) %>%
  select(all,interrupted,propInterrupt,id) %>%
  gather(key,value,-propInterrupt,-id)

toplot2 <- utterance_level %>%
  mutate(interruptor = ifelse(interruptor == 1 & interrupted == 1,1,0)) %>%
  group_by(position,party,gender,name) %>%
  summarise(all = n(),
            interruptor = sum(interruptor)) %>%
  ungroup() %>%
  mutate(propInterrupt = interruptor/all,
         all =  all - interruptor) %>%
  mutate(id = paste0(str_to_title(name),' (',position,': ',party,'-',gender,')')) %>%
  select(all,interruptor,propInterrupt,id) %>%
  gather(key,value,-propInterrupt,-id) %>%
  filter(id %in% toplot1$id)

toplot <- toplot1 %>%
  mutate(type = 'Interrupted') %>%
  bind_rows(toplot2 %>%
              mutate(type = 'Interruptor'))

pdf('./output/figures/utterances_interruptions_bar.pdf',width = 8,height = 7)
toplot1 %>%
  mutate(key = ifelse(grepl('interrupt',key),'Interruption','All')) %>%
  ggplot(aes(x = value,y = reorder(id,value),group = key,#alpha = key,
             color = key,
             fill = key,label = propInterrupt)) + 
  geom_bar(position = 'stack',stat = 'identity') + 
  geom_text(data = toplot1 %>%
              group_by(id,propInterrupt) %>%
              summarise(value = sum(value)) %>%
              mutate(propInterruptedLab = paste0(round(propInterrupt*100),'%'),
                     key = 'All'),
            color = 'black',nudge_x = 80,hjust = 0,
            aes(size = propInterrupt,label = propInterruptedLab),
            alpha = 1) +
  xlab('Total Utterances') + ylab('') + 
  scale_size_continuous(guide = 'none',range = c(2,7)) + 
  scale_x_continuous(expand = c(.15,0)) + 
  scale_fill_manual(name = '',values = c('grey80','grey30')) +
  scale_alpha_manual(name = 'Type',values = c(.5,1)) +
  scale_color_manual(name = '',values = c('white','black')) +
  theme_ridges() + 
  theme(legend.position = 'bottom',
        strip.background = element_rect(fill = 'white',color = 'black',size = .5,linetype = 'solid'),
        axis.text.y = element_text(size = 10,vjust = .5))
dev.off()

# SI
toplot1 <- utterance_level %>%
  group_by(position,party,gender,name) %>%
  summarise(all = n(),
            interrupted = sum(interrupted)) %>%
  ungroup() %>%
  mutate(propInterrupt = interrupted/all,
         all =  all - interrupted) %>%
  filter(all > 30) %>%
  mutate(id = paste0(str_to_title(name),' (',position,': ',party,'-',gender,')')) %>%
  select(all,interrupted,propInterrupt,id) %>%
  gather(key,value,-propInterrupt,-id)

pdf('./output/figures/utterances_interruptions_bar_SI.pdf',width = 8,height = 10)
toplot1 %>%
  mutate(key = ifelse(grepl('interrupt',key),'Interruption','All')) %>%
  ggplot(aes(x = value,y = reorder(id,value),group = key,#alpha = key,
             color = key,
             fill = key,label = propInterrupt)) + 
  geom_bar(position = 'stack',stat = 'identity') + 
  geom_text(data = toplot1 %>%
              group_by(id,propInterrupt) %>%
              summarise(value = sum(value)) %>%
              mutate(propInterruptedLab = paste0(round(propInterrupt*100),'%'),
                     key = 'All'),
            color = 'black',nudge_x = 80,hjust = 0,
            aes(size = propInterrupt,label = propInterruptedLab),
            alpha = 1) +
  xlab('Total Utterances') + ylab('') + 
  scale_size_continuous(guide = 'none',range = c(2,7)) + 
  scale_x_continuous(expand = c(.15,0)) + 
  scale_fill_manual(name = '',values = c('grey80','grey30')) +
  scale_alpha_manual(name = 'Type',values = c(.5,1)) +
  scale_color_manual(name = '',values = c('white','black')) +
  theme_ridges() + 
  theme(legend.position = 'bottom',
        strip.background = element_rect(fill = 'white',color = 'black',size = .5,linetype = 'solid'),
        axis.text.y = element_text(size = 8,vjust = .5))
dev.off()
